package com.liuchuanzheng.lcz_wanandroid.module.home.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.base.BaseFragment
import com.liuchuanzheng.lcz_wanandroid.databinding.FragmentHomeBinding
import com.liuchuanzheng.lcz_wanandroid.module.common.adapter.SimpleFragmentPagerAdapter

/**
 * @author 刘传政
 * @date 2021/9/14 0014 17:24
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
class HomeFragment:BaseFragment() {
    lateinit var mViewBinding: FragmentHomeBinding
    private lateinit var fragments: List<Fragment>
    /**
     * 当页面可见的时候，才加载当前页面。 没有打开的页面，就不会预加载。
     *
     *
     * 说白了，懒加载就是可见的时候才去请求数据。
     * 只回调一次
     */
    override fun onLazyLoad() {

    }

    /**
     * 因为系统自带的设置view方法有很多种,id,view都可以.所以这里不像
     * 大家都用id的封装方式,那是限制了自己
     * @param inflater
     * @param container
     */
    override fun creatView(inflater: LayoutInflater?, container: ViewGroup?): View {
        mViewBinding = FragmentHomeBinding.inflate(inflater!!, container, false)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view ,savedInstanceState)
        fragments = listOf(
            ArticleFragment(),
            ProjectCategoryFragment(),
            LatestProjectsFragment(),
        )
        val titles = listOf<CharSequence>(
            getString(R.string.popular_articles),
            getString(R.string.project_category),
            getString(R.string.latest_project),
        )
        mViewBinding.viewPager.adapter = SimpleFragmentPagerAdapter(
            fm = childFragmentManager,
            fragments = fragments,
            titles = titles
        )
        mViewBinding.viewPager.offscreenPageLimit = fragments.size
        mViewBinding.tabLayout.setupWithViewPager(mViewBinding.viewPager)
    }
}